package com.aistar.service.impl;

import com.aistar.mapper.OrderDetailsMapper;
import com.aistar.pojo.*;
import com.aistar.service.OrderDetailsService;
import com.aistar.service.ProductImagesService;
import com.aistar.service.ProductService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderDetailsServiceImpl implements OrderDetailsService {

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductImagesService productImagesService;

    //根据订单id查询到订单详情表列表
    @Override
    public ServerResponse getOrderDetailsByOrderId(Integer orderId) {
        OrderDetailsExample orderDetailsExample = new OrderDetailsExample();
        orderDetailsExample.createCriteria().andOrderIdEqualTo(orderId)
                .andOrderDetailsStatusEqualTo(1);
        List<OrderDetails> orderDetailsList = orderDetailsMapper.selectByExample(orderDetailsExample);
        if (orderDetailsList != null && orderDetailsList.size()>0){
            //有数据
            return ServerResponse.getDataSuccess(orderDetailsList);
        }else {
            return ServerResponse.getDataFailed("未查询到该订单详情");
        }

    }
    //根据orderiD 查询到 对应的详情表 在封装成一个对象
    @Override
    public ServerResponse getAllByOrderId(Integer orderId) {

        List<OrdersProductVo> ordersProductVoList = new ArrayList<>();
        OrderDetailsExample orderDetailsExample = new OrderDetailsExample();
        orderDetailsExample.createCriteria().andOrderIdEqualTo(orderId)
                .andOrderDetailsStatusEqualTo(1);
        List<OrderDetails> orderDetailsList = orderDetailsMapper.selectByExample(orderDetailsExample);
        if (orderDetailsList != null && orderDetailsList.size()>0){
            //有数据将数据显示查出来 封装到一个类里面去
            for (OrderDetails orderDetails:orderDetailsList){
                OrdersProductVo ordersProductVo = new OrdersProductVo();
                Product product = (Product) productService.getById(orderDetails.getProductId()).getData();
                //根据商品id获取到商品的图片列表
                List<ProductImages> productImagesList = (List<ProductImages>) productImagesService.getByProductId(orderDetails.getProductId()).getData();
                ProductImages productImages = productImagesList.get(0);

                ordersProductVo.setProduct(product);
                ordersProductVo.setProductCount(orderDetails.getProductCount());
                ordersProductVo.setProductImages(productImages);
                ordersProductVoList.add(ordersProductVo);
            }
            return ServerResponse.getDataSuccess(ordersProductVoList);
        }else {
            return ServerResponse.getDataFailed("未查询到该订单详情");
        }
    }

    //添加一份详情记录
    @Override
    public ServerResponse save(OrderDetails orderDetails) {
        int rows = orderDetailsMapper.insert(orderDetails);
        if (rows>0){
            return ServerResponse.addSuccess();
        }else {
            return ServerResponse.addFailed();
        }

    }

    //修改状态
    @Override
    public ServerResponse modifyStatus(OrderDetails orderDetails) {
        orderDetails.setOrderDetailsStatus(0);
        int rows = orderDetailsMapper.updateByPrimaryKeySelective(orderDetails);
        if (rows>0){
            return ServerResponse.deleteSuccess();
        }else {
            return ServerResponse.deleteFailed();
        }
    }

    @Override
    public ServerResponse modify(OrderDetails orderDetails) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id) {
       OrderDetails orderDetails = orderDetailsMapper.selectByPrimaryKey(id);
       if (orderDetails !=null){
           return ServerResponse.getDataSuccess(orderDetails);
       }else {
          return ServerResponse.getDataFailed("service 查询失败");
       }
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }
}
